home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksocketdevice.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-10-08  |  13.0 KB  |  429 lines

  1. /*  -*- C++ -*-
  2.  *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
  3.  *
  4.  *
  5.  *  Permission is hereby granted, free of charge, to any person obtaining
  6.  *  a copy of this software and associated documentation files (the
  7.  *  "Software"), to deal in the Software without restriction, including
  8.  *  without limitation the rights to use, copy, modify, merge, publish,
  9.  *  distribute, sublicense, and/or sell copies of the Software, and to
  10.  *  permit persons to whom the Software is furnished to do so, subject to
  11.  *  the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included 
  14.  *  in all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20.  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21.  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. #ifndef KSOCKETDEVICE_H
  26. #define KSOCKETDEVICE_H
  27.  
  28. #include <qsocketnotifier.h>
  29. #include "ksocketbase.h"
  30.  
  31. namespace KNetwork {
  32.  
  33. class KSocketDevice;
  34. class KSocketDeviceFactoryBase;
  35.  
  36. class KSocketDevicePrivate;
  37. /** @class KSocketDevice ksocketdevice.h ksocketdevice.h
  38.  *  @brief Low-level socket functionality.
  39.  *
  40.  * This class provides low-level socket functionality. 
  41.  *
  42.  * Most users will prefer "cooked" interfaces like those of @ref KStreamSocket or
  43.  * @ref KServerSocket.
  44.  *
  45.  * Descended classes from this one provide some other kinds of socket functionality,
  46.  * like proxying or specific socket types.
  47.  *
  48.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  49.  */
  50. class KDECORE_EXPORT KSocketDevice: public KActiveSocketBase, public KPassiveSocketBase
  51. {
  52. public:
  53.   /**
  54.    * Capabilities for the socket implementation.
  55.    *
  56.    * KSocketDevice-derived classes can implement certain capabilities that are not
  57.    * available in the default class. These capabilities are described by these flags.
  58.    * The default KSocketDevice class has none of these capabilities.
  59.    *
  60.    * For the negative capabilities (inabilities, the CanNot* forms), when a capability 
  61.    * is not present, the implementation will default to the original behaviour.
  62.    */
  63.   enum Capabilities
  64.     {
  65.       /** Can connect to hostnames.
  66.        *  If this flag is present, the string form of @ref connect can be used. */
  67.       CanConnectString = 0x01,
  68.  
  69.       /** Can bind to hostnames.
  70.        *  If this flag is present, the string form of @ref bind can be used */
  71.       CanBindString = 0x02,
  72.  
  73.       /** Can not bind.
  74.        *  If this flag is present, this implementation cannot bind */
  75.       CanNotBind = 0x04,
  76.  
  77.       /** Can not listen.
  78.        *  If this flag is present, this implementation cannot listen */
  79.       CanNotListen = 0x08,
  80.  
  81.       /**
  82.        * Can send multicast as well as join/leave multicast groups.
  83.        */
  84.       CanMulticast = 0x10,
  85.  
  86.       /**
  87.        * Can not use datagrams.
  88.        * Note that this implies multicast capability not being available either.
  89.        */
  90.       CanNotUseDatagrams = 0x20
  91.     };
  92. protected:
  93.   /// The socket file descriptor. It is used throughout the implementation
  94.   /// and subclasses.
  95.   int m_sockfd;
  96.  
  97. public:
  98.   /**
  99.    * Default constructor.
  100.    *
  101.    * The parameter is used to specify which socket this object is used as
  102.    * a device for.
  103.    */
  104.   explicit KSocketDevice(const KSocketBase* = 0L);
  105.  
  106.   /**
  107.    * Constructs a new object around an already-open socket.
  108.    *
  109.    * Note: you should write programs that create sockets through
  110.    * the classes whenever possible.
  111.    */
  112.   explicit KSocketDevice(int fd);
  113.  
  114.   /**
  115.    * Destructor. This closes the socket if it's open.
  116.    */
  117.   virtual ~KSocketDevice();
  118.  
  119.   /**
  120.    * Returns the file descriptor for this socket.
  121.    */
  122.   inline int socket() const
  123.   { return m_sockfd; }
  124.  
  125.   /**
  126.    * Returns the set of capabilities this socket class implements.
  127.    * The set of capabilities is defined as an OR-ed mask of 
  128.    * @ref Capabilities bits.
  129.    *
  130.    * The default implementation is guaranteed to always return 0. That
  131.    * is, derived implementations always return bits where they differ
  132.    * from the system standard sockets.
  133.    */
  134.   virtual int capabilities() const
  135.   { return 0; }
  136.  
  137.   /**
  138.    * This implementation sets the options on the socket.
  139.    */
  140.   virtual bool setSocketOptions(int opts);
  141.  
  142.   /**
  143.    * Reimplementation from QIODevice. You should not call this function in sockets.
  144.    */
  145.   virtual bool open(int mode);
  146.  
  147.   /**
  148.    * Closes the socket. Reimplemented from QIODevice.
  149.    *
  150.    * Use this function to close the socket this object is holding open.
  151.    */
  152.   virtual void close();
  153.  
  154.   /**
  155.    * This call is not supported on sockets. Reimplemented from QIODevice.
  156.    */
  157.   virtual void flush()
  158.   { }
  159.  
  160.   /**
  161.    * Creates a socket but don't connect or bind anywhere.
  162.    * This function is the equivalent of the system call socket(2).
  163.    */
  164.   virtual bool create(int family, int type, int protocol);
  165.  
  166.   /**
  167.    * @overload
  168.    * Creates a socket but don't connect or bind anywhere.
  169.    */
  170.   bool create(const KResolverEntry& address);
  171.  
  172.   /**
  173.    * Binds this socket to the given address.
  174.    */
  175.   virtual bool bind(const KResolverEntry& address);
  176.  
  177.   /**
  178.    * Puts this socket into listening mode.
  179.    */
  180.   virtual bool listen(int backlog = 5);    // 5 is arbitrary
  181.  
  182.   /**
  183.    * Connect to a remote host.
  184.    */
  185.   virtual bool connect(const KResolverEntry& address);
  186.  
  187.   /**
  188.    * Accepts a new incoming connection.
  189.    * Note: this function returns a socket of type KSocketDevice.
  190.    */
  191.   virtual KSocketDevice* accept();
  192.  
  193.   /**
  194.    * Disconnects this socket.
  195.    */
  196.   virtual bool disconnect();
  197.  
  198.   /**
  199.    * Returns the number of bytes available for reading without blocking.
  200.    */
  201.   virtual Q_LONG bytesAvailable() const;
  202.  
  203.   /**
  204.    * Waits up to @p msecs for more data to be available on this socket.
  205.    *
  206.    * This function is a wrapper against @ref poll. This function will wait
  207.    * for any read events.
  208.    */
  209.   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L);
  210.  
  211.   /**
  212.    * Reads data from this socket.
  213.    */
  214.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
  215.  
  216.   /**
  217.    * Reads data and the source address from this socket.
  218.    */
  219.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from);
  220.  
  221.   /**
  222.    * Peeks data in the socket.
  223.    */
  224.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen);
  225.  
  226.   /**
  227.    * Peeks the data in the socket and the source address.
  228.    */
  229.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from);
  230.  
  231.   /**
  232.    * Writes data to the socket.
  233.    */
  234.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
  235.  
  236.   /**
  237.    * Writes the given data to the given destination address.
  238.    */
  239.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to);
  240.  
  241.   /**
  242.    * Returns this socket's local address.
  243.    */
  244.   virtual KSocketAddress localAddress() const;
  245.  
  246.   /**
  247.    * Returns this socket's peer address. If this implementation does proxying
  248.    * of some sort, this is the real external address, not the proxy's address.
  249.    */
  250.   virtual KSocketAddress peerAddress() const;
  251.  
  252.   /**
  253.    * Returns this socket's externally visible local address.
  254.    *
  255.    * If this socket has a local address visible externally different
  256.    * from the normal local address (as returned by @ref localAddress), then
  257.    * return it.
  258.    *
  259.    * Certain implementations will use proxies and thus have externally visible
  260.    * addresses different from the local socket values. The default implementation
  261.    * returns the same value as @ref localAddress.
  262.    *
  263.    * @note This function may return an empty KSocketAddress. In that case, the
  264.    *       externally visible address could/can not be determined.
  265.    */
  266.   virtual KSocketAddress externalAddress() const;
  267.  
  268.   /**
  269.    * Returns a socket notifier for input on this socket.
  270.    * The notifier is created only when requested. Whether
  271.    * it is enabled or not depends on the implementation.
  272.    *
  273.    * This function might return NULL.
  274.    */
  275.   QSocketNotifier* readNotifier() const;
  276.  
  277.   /**
  278.    * Returns a socket notifier for output on this socket.
  279.    * The is created only when requested.
  280.    *
  281.    * This function might return NULL.
  282.    */
  283.   QSocketNotifier* writeNotifier() const;
  284.  
  285.   /**
  286.    * Returns a socket notifier for exceptional events on this socket.
  287.    * The is created only when requested.
  288.    *
  289.    * This function might return NULL.
  290.    */
  291.   QSocketNotifier* exceptionNotifier() const;
  292.  
  293.   /**
  294.    * Executes a poll in the socket, via select(2) or poll(2).
  295.    * The events polled are returned in the parameters pointers.
  296.    * Set any of them to NULL to disable polling of that event.
  297.    *
  298.    * On exit, @p input, @p output and @p exception will contain
  299.    * true if an event of that kind is waiting on the socket or false
  300.    * if not. If a timeout occurred, set @p timedout to true (all other
  301.    * parameters are necessarily set to false).
  302.    *
  303.    * @param input    if set, turns on polling for input events
  304.    * @param output    if set, turns on polling for output events
  305.    * @param exception    if set, turns on polling for exceptional events
  306.    * @param timeout    the time in milliseconds to wait for an event;
  307.    *            0 for no wait and any negative value to wait forever
  308.    * @param timedout    on exit, will contain true if the polling timed out
  309.    * @return true if the poll call succeeded and false if an error occurred
  310.    */
  311.   virtual bool poll(bool* input, bool* output, bool* exception = 0L,
  312.             int timeout = -1, bool* timedout = 0L);
  313.  
  314.   /**
  315.    * Shorter version to poll for any events in a socket. This call
  316.    * polls for input, output and exceptional events in a socket but
  317.    * does not return their states. This is useful if you need to wait for
  318.    * any event, but don't need to know which; or for timeouts.
  319.    *
  320.    * @param timeout    the time in milliseconds to wait for an event;
  321.    *            0 for no wait and any negative value to wait forever
  322.    * @param timedout    on exit, will contain true if the polling timed out
  323.    * @return true if the poll call succeeded and false if an error occurred
  324.    */
  325.   bool poll(int timeout = -1, bool* timedout = 0L);
  326.  
  327. protected:
  328.   /**
  329.    * Special constructor. This constructor will cause the internal
  330.    * socket device NOT to be set. Use this if your socket device class
  331.    * takes another underlying socket device.
  332.    *
  333.    * @param parent    the parent, if any
  334.    */
  335.   KSocketDevice(bool, const KSocketBase* parent = 0L);
  336.  
  337.   /**
  338.    * Creates a socket notifier of the given type.
  339.    *
  340.    * This function is called by @ref readNotifier, @ref writeNotifier and
  341.    * @ref exceptionNotifier when they need to create a socket notifier
  342.    * (i.e., the first call to those functions after the socket is open).
  343.    * After that call, those functions cache the socket notifier and will
  344.    * not need to call this function again.
  345.    *
  346.    * Reimplement this function in your derived class if your socket type
  347.    * requires a different kind of QSocketNotifier. The return value should
  348.    * be deleteable with delete. (@ref close deletes them).
  349.    *
  350.    * @param type    the socket notifier type
  351.    */
  352.   virtual QSocketNotifier* createNotifier(QSocketNotifier::Type type) const;
  353.  
  354. public:
  355.   /**
  356.    * Creates a new default KSocketDevice object given
  357.    * the parent object.
  358.    *
  359.    * The capabilities flag indicates the desired capabilities the object being
  360.    * created should possess. Those capabilities are not guaranteed: if no factory
  361.    * can provide such an object, a default object will be created.
  362.    *
  363.    * @param parent    the KSocketBase parent
  364.    */
  365.   static KSocketDevice* createDefault(KSocketBase* parent);
  366.  
  367.   /**
  368.    * @overload
  369.    *
  370.    * This will create an object only if the requested capabilities match.
  371.    *
  372.    * @param parent        the parent
  373.    * @param capabilities    the requested capabilities
  374.    */
  375.   static KSocketDevice* createDefault(KSocketBase* parent, int capabilities);
  376.  
  377.   /**
  378.    * Sets the default KSocketDevice implementation to use and
  379.    * return the old factory.
  380.    *
  381.    * @param factory    the factory object for the implementation
  382.    */
  383.   static KSocketDeviceFactoryBase* setDefaultImpl(KSocketDeviceFactoryBase* factory);
  384.  
  385.   /**
  386.    * Adds a factory of KSocketDevice objects to the list, along with its
  387.    * capabilities flag.
  388.    */
  389.   static void addNewImpl(KSocketDeviceFactoryBase* factory, int capabilities);
  390.  
  391. private:
  392.   KSocketDevice(const KSocketDevice&);
  393.   KSocketDevice& operator=(const KSocketDevice&);
  394.  
  395.   KSocketDevicePrivate *d;
  396. };
  397.  
  398. /** @internal
  399.  * This class provides functionality for creating and registering
  400.  *  socket implementations.
  401.  */
  402. class KSocketDeviceFactoryBase
  403. {
  404. public:
  405.   KSocketDeviceFactoryBase() {}
  406.   virtual ~KSocketDeviceFactoryBase() {}
  407.  
  408.   virtual KSocketDevice* create(KSocketBase*) const = 0;
  409. };
  410.  
  411. /**
  412.  * This class provides functionality for creating and registering 
  413.  * socket implementations.
  414.  */
  415. template<class Impl>
  416. class KSocketDeviceFactory: public KSocketDeviceFactoryBase
  417. {
  418. public:
  419.   KSocketDeviceFactory() {}
  420.   virtual ~KSocketDeviceFactory() {}
  421.  
  422.   virtual KSocketDevice* create(KSocketBase* parent) const
  423.   { return new Impl(parent); }
  424. };
  425.  
  426. }                // namespaces
  427.  
  428. #endif
  429.